package itchen.com.inputdemo;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.yanzhenjie.alertdialog.AlertDialog;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.util.List;

public class RationalePermissionActivity extends AppCompatActivity implements PermissionListener {


	private static final int REQUEST_CODE_PERMISSION_LOCATION = 100;
	private static final int REQUEST_CODE_SETTING = 300;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_rationale_permission);


		findViewById(R.id.btn_request_location).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				if (AndPermission.hasPermission(RationalePermissionActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)) {

					Toast.makeText(RationalePermissionActivity.this, "定位权限已经有了", Toast.LENGTH_SHORT).show();

				} else {
					AndPermission.with(RationalePermissionActivity.this)//
							.requestCode(REQUEST_CODE_PERMISSION_LOCATION)//
							.permission(Manifest.permission.ACCESS_FINE_LOCATION)//
							.rationale(mRationaleListener)//
							.send();//发送该权限验证
				}

			}
		});

	}

	private RationaleListener mRationaleListener = new RationaleListener() {
		@Override
		public void showRequestPermissionRationale(int requestCode, final Rationale rationale) {

			AlertDialog.build(RationalePermissionActivity.this).setTitle("友情提示")
					.setMessage(R.string.message_permission_rationale)
					.setPositiveButton(R.string.btn_dialog_yes_permission, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
							rationale.resume();
						}
					}).setNegativeButton(R.string.btn_dialog_no_permission, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
					rationale.cancel();
				}
			}).show();

		}
	};

	@Override
	public void onSucceed(int requestCode, List<String> grantPermissions) {

		switch (requestCode) {
			case REQUEST_CODE_PERMISSION_LOCATION: {
				Toast.makeText(this, R.string.message_location_succeed, Toast.LENGTH_SHORT).show();
				break;
			}
		}

	}

	@Override
	public void onFailed(int requestCode, List<String> deniedPermissions) {

		switch (requestCode) {
			case REQUEST_CODE_PERMISSION_LOCATION: {
				Toast.makeText(this, R.string.message_location_failed, Toast.LENGTH_SHORT).show();
				break;
			}
		}

		// 用户否勾选了不再提示并且拒绝了权限，那么提示用户到设置中授权。
		if (AndPermission.hasAlwaysDeniedPermission(this, deniedPermissions)) {
			// 第一种：用默认的提示语。
			AndPermission.defaultSettingDialog(this, REQUEST_CODE_SETTING).show();

			// 第二种：用自定义的提示语。
			//             AndPermission.defaultSettingDialog(this, REQUEST_CODE_SETTING)
			//                     .setTitle("权限申请失败")
			//                     .setMessage("我们需要的一些权限被您拒绝或者系统发生错误申请失败，请您到设置页面手动授权，否则功能无法正常使用！")
			//                     .setPositiveButton("好，去设置")
			//                     .show();

			//            第三种：自定义dialog样式。
			//            SettingService settingHandle = AndPermission.defineSettingDialog(this, REQUEST_CODE_SETTING);
			//            你的dialog点击了确定调用：
			//            settingHandle.execute();
			//            你的dialog点击了取消调用：
			//            settingHandle.cancel();
		}
	}

	@Override
	public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]
			grantResults) {
		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
		// listener方式，最后一个参数是PermissionListener。
		AndPermission.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
			case REQUEST_CODE_SETTING: {
				Toast.makeText(this, R.string.message_setting_back, Toast.LENGTH_LONG).show();
				break;
			}
		}
	}

}
